﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HCFramework.API.LINQ;

namespace C
{
    public class ShoppingCart
    {
        public static string ConvertToString(List<CartItem> list)
        {
            if (list == null || list.Count < 1) return "";
            string cookie = "";
            foreach (CartItem item in list)
            {
                cookie += "~" + item.Count + "*" + item.CurrencyCode + "*" + item.ImageUrl + "*" + item.Price + "*" + item.ProductCode + "*" + item.ProductId + "*" + item.ProductName;
            }
            cookie = cookie.TrimStart('~');
            return cookie;
        }

        public static List<CartItem> ConvertToList(string cookie)
        {
            List<CartItem> ret = new List<CartItem>();
            if (cookie == null || cookie.Trim() == "") return ret;
            cookie = HttpUtility.UrlDecode(cookie);
            foreach (string item in cookie.Split('~'))
            {
                CartItem cartItem = new CartItem();

                cartItem.Count = int.Parse(item.Split('*')[0]);
                cartItem.CurrencyCode = item.Split('*')[1];
                cartItem.ImageUrl = item.Split('*')[2];
                cartItem.Price = decimal.Parse(item.Split('*')[3]);
                cartItem.ProductCode = item.Split('*')[4];
                cartItem.ProductId = int.Parse(item.Split('*')[5]);
                cartItem.ProductName = item.Split('*')[6];

                ret.Add(cartItem);
            }
            return ret;
        }

        public static List<CartItem> ListCartItem
        {
            get
            {
                string cookie = M.Common.GetUserData("ShoppingCartCookie");
                return ConvertToList(cookie);
            }
            set
            {
                string cookie = ConvertToString(value);
                M.Common.SetUserData("ShoppingCartCookie", cookie);
            }
        }

        public static void UpdateSession()
        {
            M.Common.UpdateSession("ShoppingCartCookie");
        }

        public static void Add(int count, int productId, string productName, string productCode, decimal price, string imageUrl, string currencyCode)
        {
            List<CartItem> list = ListCartItem;
            CartItem item = list.Where(ik => ik.ProductId == productId).FirstOrDefault();
            if (item != null)
            {
                Update(item.ProductId, count);
            }
            else
            {
                item = new CartItem();
                item.Count = count;
                item.CurrencyCode = currencyCode;
                item.ImageUrl = imageUrl;
                item.Price = price;
                item.ProductCode = productCode;
                item.ProductId = productId;
                item.ProductName = productName;
                list.Add(item);

                M.Common.SetUserData("ShoppingCartCookie", ConvertToString(list));
            }
        }

        public static void Add(int productId, int count)
        {
            Product p = C.ProductController.GetByID(productId);
            Add(count, p.ProductID, p.ProductName, p.ProductCode, p.Price, C.ProductImageController.GetMainImage(p).URL, p.CurrencyCode);
        }

        public static void Remove(int productId)
        {
            List<CartItem> list = ListCartItem;
            CartItem item = list.Where(ik => ik.ProductId == productId).FirstOrDefault();
            if (item != null) list.Remove(item);
            M.Common.SetUserData("ShoppingCartCookie", ConvertToString(list));
        }

        public static void Clear()
        {
            List<CartItem> list = ListCartItem;
            list.Clear();
            M.Common.SetUserData("ShoppingCartCookie", ConvertToString(list));
        }

        public static void Update(int productId, int count)
        {
            List<CartItem> list = ListCartItem;
            CartItem item = list.Where(ik => ik.ProductId == productId).FirstOrDefault();
            if (item != null) item.Count = count;
            M.Common.SetUserData("ShoppingCartCookie", ConvertToString(list));
        }
    }

    public class CartItem
    {
        public int Count { get; set; }
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public string ProductCode { get; set; }
        public decimal Price { get; set; }
        public string ImageUrl { get; set; }
        public string CurrencyCode { get; set; }
    }
}